<?php

/**
  ----------------------------------------------------------
 * @4A平台管理系统
  ----------------------------------------------------------
 * @copyright Copyright @ 2012 By www.4a.cc
  ----------------------------------------------------------
 * @author Yang Changwu<flash165@qq.com> 2012-04-14
  ----------------------------------------------------------
 * @version 0.1
  ----------------------------------------------------------
 */
class yeepay {

    /**
      +----------------------------------------------------------
     * 易宝充值处理
      +----------------------------------------------------------
     * @access public
     * @name BeginPay
     * @category   yeepay
     * @subpackage  Pay
     * @param Array
     * @return array
     * @author Yang Changwu<flash165@qq.com> 2012-05-10
      +----------------------------------------------------------
     */
    public function BeginPay($arr = array(), $bankname = '') {
        //var_dump($arr);exit;
        #	商家设置用户购买商品的支付信息.
        #       易宝支付平台统一使用GBK/GB2312编码方式,参数如用到中文，请注意转码
        include 'yeepay/merchantProperties.php';
        include 'yeepay/yeepayCommon.php';
        //请求地址 测试专用
        //$data ['reqURL_onLine'] = 'http://tech.yeepay.com:8080/robot/debug.action';
        #           提交请求地址
        $data ['reqURL_onLine'] = $reqURL_onLine;

        #           接口类型
        $data ['p0_Cmd'] = $p0_Cmd;

        #           商户密钥
        $data ['p1_MerId'] = $p1_MerId;

        #	商户订单号,选填.
        ##若不为""，提交的订单号必须在自身账户交易中唯一;为""时，易宝支付会自动生成随机的商户订单号.
        $data ['p2_Order'] = $arr['order_num'];
        #	支付金额,必填.
        ##单位:元，精确到分.
        $data ['p3_Amt'] = $arr['pay_cny'];

        #	交易币种,固定值"CNY".
        $data ['p4_Cur'] = "CNY";

        #	商品名称
        ##用于支付时显示在易宝支付网关左侧的订单产品信息.
        $data ['p5_Pid'] = iconv("UTF-8", "GBK", $arr['discript']);

        #	商品种类
        $data ['p6_Pcat'] = iconv("UTF-8", "GBK", $arr['discript']);

        #	商品描述
        $data ['p7_Pdesc'] = '';

        #	商户接收支付成功数据的地址,支付成功后易宝支付会向该地址发送两次成功通知.
        $data ['p8_Url'] = 'http://www.248game.com/index.php?s=/pay/callback/paytag/yeepay/';

        #	为“1”: 需要用户将送货地址留在易宝支付系统; 为“0”: 不需要，默认为 ”0”
        $data ['p9_SAF'] = $p9_SAF;

        #	商户扩展信息
        ##商户可以任意填写1K 的字符串,支付成功时将原样返回.
        $data ['pa_MP'] = "";

        #	银行编码
        ##默认为""，到易宝支付网关.若不需显示易宝支付的页面，直接跳转到各银行、神州行支付、骏网一卡通等支付页面，该字段可依照附录:银行列表设置参数值.
        $data ['pd_FrpId'] = $bankname;

        #	应答机制
        ##为"1": 需要应答机制;为"0": 不需要应答机制.
        $data ['pr_NeedResponse'] = "1";

        $data ['hmac'] = getReqHmacString($data ['p2_Order'], $data ['p3_Amt'], $data ['p4_Cur'], $data ['p5_Pid'], $data ['p6_Pcat'], $data ['p7_Pdesc'], $data ['p8_Url'], $data ['pa_MP'], $data ['pd_FrpId'], $data ['pr_NeedResponse']);

        return $data;
    }

    /**
      +----------------------------------------------------------
     * 易宝充值回调处理
      +----------------------------------------------------------
     * @access public
     * @name CallbackPay
     * @category   yeepay
     * @subpackage  Pay
     * @param Array
     * @return array
     * @author Yang Changwu<flash165@qq.com> 2012-05-10
      +----------------------------------------------------------
     */
    public function CallbackPay($arr = array(), $payorder = array(), \Home\Model\PayModel $paymodel) {
        require_once 'yeepay/merchantProperties.php';
        include 'yeepay/yeepayCommon.php';
        //易宝返回数据GBK  需处理为UTF-8
        $arr['r5_Pid'] = iconv("GBK", "UTF-8", $arr['r5_Pid']); //神仙道|双线1服
        $yeepay_num = $arr['p1_MerId']; //10011871473
        $pay_num = $p1_MerId;
        //使用易宝接口多渠道处理
        $cardarr[] = 'SNDACARD-NET';    //盛大卡
        $cardarr[] = 'ZHENGTU-NET';    //征途卡
        $cardarr[] = 'JIUYOU-NET';    //久游卡
        $cardarr[] = 'YPCARD-NET';    //易宝e卡通
        $cardarr[] = 'NETEASE-NET';    //网易卡
        $cardarr[] = 'WANMEI-NET';    //完美卡
        $cardarr[] = 'SOHU-NET';    //搜狐卡
        $cardarr[] = 'ZONGYOU-NET';    //纵游一卡通
        $cardarr[] = 'TIANXIA-NET';    //天下一卡通
        $cardarr[] = 'TIANHONG-NET';    //天宏一卡通
        $cardarr[] = 'BESTPAY-NET';    //翼支付
        //$cardarr[] = 'JUNNET-NET';    //骏网一卡通
        //$cardarr[] = 'SZX-NET';    //神州行
        //$cardarr[] = 'QQCARD-NET';    //Q币卡
        //$cardarr[] = 'UNICOM-NET';    //联通卡
        //$cardarr[] = 'TELECOM-NET';    //电信卡
        if (in_array($arr['rb_BankId'], $cardarr)) {
            $arr['paytag'] = 'kapay';
        } else {
            switch ($arr['rb_BankId']) {
                case 'SZX-NET':
                    $get['paytag'] = 'szxpay';    //神州行
                    break;
                case 'UNICOM-NET':
                    $get['paytag'] = 'ltkpay';    //联通卡
                    break;
                case 'TELECOM-NET':
                    $get['paytag'] = 'dxkpay';    //电信卡
                    break;
                case 'JUNNET-NET':
                    $get['paytag'] = 'jwkpay';    //骏网一卡通
                    break;
                case 'QQCARD-NET':
                    $get['paytag'] = 'qbkpay';     //Q币卡
                    break;
                default :
                    $arr['paytag'] = $arr['paytag'];
                    break;
            }
        }
        //转化
        extract($arr);

        #	只有支付成功时易宝支付才会通知商户.
        ##支付成功回调有两次，都会通知到在线支付请求参数中的p8_Url上：浏览器重定向;服务器点对点通讯.
        #	解析返回参数.
        $return = getCallBackValue($r0_Cmd, $r1_Code, $r2_TrxId, $r3_Amt, $r4_Cur, $r5_Pid, $r6_Order, $r7_Uid, $r8_MP, $r9_BType, $hmac);

        #	判断返回签名是否正确（True/False）
        $bRet = CheckHmac($r0_Cmd, $r1_Code, $r2_TrxId, $r3_Amt, $r4_Cur, $r5_Pid, $r6_Order, $r7_Uid, $r8_MP, $r9_BType, $hmac);
        #	以上代码和变量不需要修改.
        #	校验码正确.
        if ($bRet) {
            if ($r1_Code == "1") {
                # 需要比较返回的金额与商家数据库中订单的金额是否相等，只有相等的情况下才认为是交易成功.
                # 并且需要对返回的处理进行事务控制，进行记录的排它性处理，防止对同一条交易重复发货的情况发生.
                #“1”浏览器返回
                if ($r9_BType == "1") {
                    //判断订单号
                    //判断金额
                    if ($r3_Amt != $payorder['pay_cny']) {
                        return array(
                            'ret' => 1,
                            'msg' => '充值金额与订单金额不符！',
                        );
                    }
                    //判断商户号
                    if ($yeepay_num != $pay_num) {
                        return array(
                            'ret' => 2,
                            'msg' => '商户号不正确！',
                        );
                    }
                    //判断成功状态
                    if ($payorder['flag'] == '1') {
                        die('success');
                    }
                    if ($paymodel->setByOrderInfo($arr['r6_Order'], $arr['ro_BankOrderId'], $arr['ru_Trxtime'], 1)) {
                        return array(
                            'ret' => 0,
                            'msg' => '正确',
                        );
                    }
                    return array(
                        'ret' => 3,
                        'msg' => '充值失败，请联系客服处理！',
                    );
                    #“2”服务器返回。
                } elseif ($r9_BType == "2") {
                    //判断金额
                    if ($r3_Amt != $payorder['pay_cny']) {
                        return array(
                            'ret' => 1,
                            'msg' => '充值金额与订单金额不符！',
                        );
                    }
                    //判断商户号
                    if ($yeepay_num != $pay_num) {
                        return array(
                            'ret' => 2,
                            'msg' => '商户号不正确！',
                        );
                    }
                    //判断成功状态
                    if ($payorder['flag'] == '1') {
                        die('success');
                    }
                    if ($paymodel->setByOrderInfo($arr['r6_Order'], $arr['ro_BankOrderId'], $arr['ru_Trxtime'], 1)) {
                        return array(
                            'ret' => 0,
                            'msg' => '正确',
                        );
                    }
                    return array(
                        'ret' => 3,
                        'msg' => '充值失败，请联系客服处理！',
                    );
                    #如果需要应答机制则必须回写流,以success开头,大小写不敏感.
                } elseif ($r9_BType == "3") {
                    return array(
                        'ret' => 3,
                        'msg' => '充值失败，请联系客服处理！',
                    );
                }
            }
        } else {
            return array(
                'ret' => 4,
                'msg' => '交易信息被篡改，请联系客服处理！',
            );
        }
    }

}

?>